<?php
	/*
	 * Vuong Minh Chau <mchau2083@yahoo.com>
	*/
?>
<?php

$CI =& get_instance();
$CI->load->library('pagination');
$CI->load->model('Model_news', 'model_news');
$CI->load->helper('date');
$month = "";
$segments = $CI->uri->segments;
$get_conditional = $this->uri->segment(4, 'lastest_news');

$cur_month = date("m")*1;
$cur_year  = date("Y");
if($cur_month == 1){
	$month_2 = 12;
	$year_2 = $cur_year - 1;
}else{
	$month_2 = $cur_month-1;
	$year_2 = $cur_year;
}

if($month_2 == 1){
	$month_3 = 12;
	$year_3 = $cur_year - 1;
}else{
	$month_3 = $month_2-1;
	$year_3 = $cur_year;
}
switch($get_conditional){
	case 'lastest_news':
		$list_count = $CI->model_news->browse_count('','','','');
		$limit = 2;
		$_GET['per_page'] = $this->uri->segment(5,0);
		$per_page = isset($_GET['per_page']) ? intval($_GET['per_page']):0;

		$start = 0;

		if($per_page){
			$start = $per_page;
		}

		$list = $CI->model_news->browse('','','','',$limit,$start,'postdate','desc');
		$list_othernews = $CI->model_news->browse('','','','',$limit,$start+$limit,'postdate','desc');

		$config['base_url'] = site_url(array('c'=>'home','m'=>'index','nav'=>$node['id'],'get'=>'lastest_news'));
		$config['uri_segment'] = 5;
		$config['total_rows'] = $list_count;
		$config['per_page'] = $limit;
		$CI->pagination->initialize($config);
		break;


	case 'in_3_month':
		$month = $this->uri->segment(5,$cur_month);
		$f_date = date("Y-m-d", strtotime($month.'/01/'.$cur_year.' 00:00:00'));
		$l_date = date("Y-m-d", strtotime('-1 second',strtotime('+1 month',strtotime($month.'/01/'.$cur_year.' 00:00:00'))));

		$list_count = $CI->model_news->browse_count('','',$f_date,$l_date);
		$limit = 2;
		$_GET['per_page'] = $this->uri->segment(6,0);
		$per_page = isset($_GET['per_page']) ? intval($_GET['per_page']):0;
		$start = 0;
		if($per_page){
			$start = $per_page;
		}

		$list = $CI->model_news->browse('','',$f_date,$l_date,$limit,$start,'postdate','desc');
		$list_othernews = $CI->model_news->browse('','',$f_date,$l_date,$limit,$start+$limit,'postdate','desc');

		break;

	default:
		$f_date = date("Y-m-d", strtotime(date('m').'/01/'.date('Y').' 00:00:00'));
		$l_date = date("Y-m-d", strtotime('-1 second',strtotime('+1 month',strtotime(date('m').'/01/'.date('Y').' 00:00:00'))));
		$list = $CI->model_news->browse('','',$f_date,$l_date,6,0,'postdate','desc');
		$list_othernews = $CI->model_news->browse('','',$f_date,$l_date,6,6,'postdate','desc');
		break;
}

$t1 = unserialize($content['t1']);

?>
<div class="left-2-col texture">
	<ul class="bullet_list">
		<li class="<?=($get_conditional=='lastest_news')?'selected':''?>"><a href="<?=site_url(array('c' => 'home','m' => 'index', 'nav'=>$node['id'], 'get'=> 'lastest_news'))?>"><?=$this->lang->line('Lastest News')?></a></li>
		<li class="<?=($get_conditional=='in_3_month')?'selected':''?>">
			<a href="<?=site_url(array('c' => 'home','m' => 'index', 'nav'=>$node['id'], 'get'=> 'in_3_month'))?>"><?=$this->lang->line('In the last 3 months')?></a>
			<ul class="last_3month">
				<li class="<?=($month==$cur_month)?'selected':''?>"><a href="<?=site_url(array('c' => 'home','m' => 'index', 'nav'=>$node['id'], 'get'=> 'in_3_month', 'month'=>$cur_month))?>"><?=$cur_month."-". $cur_year?></a></li>
				<li class="<?=($month==$month_2)?'selected':''?>"><a href="<?=site_url(array('c' => 'home','m' => 'index', 'nav'=>$node['id'], 'get'=> 'in_3_month', 'month'=>$month_2))?>"><?=$month_2."-". $year_2?></a></li>
				<li class="<?=($month==$month_3)?'selected':''?>"><a href="<?=site_url(array('c' => 'home','m' => 'index', 'nav'=>$node['id'], 'get'=> 'in_3_month', 'month'=>$month_3))?>"><?=$month_3."-". $year_3?></a></li>
			</ul>
		</li>
		<!--<li class="<?=($get_conditional=='older')?'selected':''?>"><a href="<?=site_url(array('c' => 'home','m' => 'index', 'nav'=>$node['id'], 'get'=> 'older'))?>">Older articles</a></li>-->
	</ul>
</div>
<div class="col_center" id="page_search">
	<h2><?= htmlspecialchars($node['nav_name'])?></h2>
	<ul class="news-grid">
	<?php if(!empty($list)):?>
	<?php
			foreach($list as $row){
				if(file_exists(base_url().$row['img'])=== false){
					$file = userfile_img_url($row['img']);
				}else
					$file = base_url().'userfiles/image/noimage.png';
	?>
				<li class="item">
					<div class="content" style="min-height:120px;">
						<div class="image_border"><a href="<?=site_url(array('c' => 'home','m' => 'news_detail', 'nav'=>$node['id'] ,'id' => $row['id'],'action'=>$get_conditional))?>"><img src="<?=$file?>" width="120" height="80" alt=""/></a></div>
						<span class="title"><a href="<?=site_url(array('c' => 'home','m' => 'news_detail', 'nav'=>$node['id'] ,'id' => $row['id'],'action'=>$get_conditional))?>" title="<?=htmlspecialchars($row['name'])?>"><?=htmlspecialchars($row['name'])?></a></span><br />
						<?php

								$datestring = "%M %d, %Y %h:%i %A";
								$time = strtotime($row['postdate']);
						?>
						<span class="create_date"><?= mdate($datestring, $time); ?></span><br />
						<span class="content"><?=word_limiter($row['brief'],20)?></span>
					</div>
					<div class="read_more"><a href="<?=site_url(array('c' => 'home','m' => 'news_detail', 'nav'=>$node['id'] ,'id' => $row['id'],'action'=>$get_conditional))?>" title="<?=htmlspecialchars($this->lang->line('Read more'))?>"><?=$this->lang->line('Read more')?></a></div>
				</li>
	<?php	}
	?>
	<?php endif;?>
	</ul>
	<div class="clearfix"></div>
	<?php if(!empty($list_othernews)):?>
	<div class="more_new" style="position:relative;">
		<div style="color:#ED1C24; background:#FFF; padding-right:2px; display:inline;"><?=$this->lang->line('More news')?></div>
		<div class="" style="position:absolute; z-index:-1; top:-3px; border-bottom:1px solid #CCC; display:inline-block; width:650px; right:0px;">&nbsp;</div>
		<div class="clearfix"></div>
		<div>
			<ul>
			<?php foreach($list_othernews as $news_other):
					$datestring = "%M %d, %Y %h:%i %A";
					$time = strtotime($news_other['postdate']);
			?>
				<li><span class="title"><a href="<?=site_url(array('c' => 'home','m' => 'news_detail', 'nav'=>$node['id'] ,'id' => $news_other['id'],'action'=>$get_conditional))?>"><?=$news_other['name']?></a></span> <span class="create_date"><?= mdate($datestring, $time); ?></span></li>
			<?php endforeach;?>
			</ul>
		</div>
	</div>
	<?php endif;?>
	<div class="pagination"><?=$CI->pagination->create_links();?></div>
</div>
<div class="clearfix"></div>